<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-row style="height: 60px">
              <el-button type="primary" icon="el-icon-plus" size="small" @click="showDialog = true">新增角色</el-button>
            </el-row>
            <el-table border="" :data="list">
              <el-table-column align="center" type="index" label="序号" width="120" />
              <el-table-column align="center" prop="name" label="角色名称" width="240" />
              <el-table-column align="center" prop="description" label="描述" />
              <el-table-column align="center" label="操作">
                <template slot-scope="{ row }">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <el-pagination
                layout="prev, pager, next"
                :page-size="page.pagesize"
                :current-page="page.page"
                :total="page.total"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <el-alert type="info" :show-icon="true" :closable="false" title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改" />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="电话">
                <el-input v-model="formData.companyPhone" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" disabled style="width: 400px" type="textarea" :rows="3" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- close事件在点击确定时也会触发 -->
      <el-dialog title="编辑部门" :visible="showDialog" @close="cancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item prop="name" label="角色名称">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <el-row type="flex" justify="center">
          <el-col :span="6">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="roleSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <!-- 分配权限 -->
      <el-dialog title="分配权限" :visible="showPerDialog" @close="permCancel">
        <el-tree
          ref="permTree"
          :data="permData"
          :props="defaultProps"
          node-key="id"
          :default-expand-all="true"
          :default-checked-keys="selectCheck"
          :show-checkbox="true"
          :check-strictly="true"
        />  <!-- 父子节点不互相关联 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" @click="permSubmit">确定</el-button>
            <el-button @click="permCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { mapGetters } from 'vuex'
import { getRequiredRules } from '@/utils/validate'
import { tranListToTreeData } from '@/utils'

export default {
  data() {
    return {
      list: [],
      page: {
        page: 1,
        pagesize: 4,
        total: 0 // 总数
      },
      formData: {}, // 公司信息
      showDialog: false, // 新增弹出层
      showPerDialog: false, // 分配权限弹出层
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [getRequiredRules('角色名称')]
      },
      permData: [],
      defaultProps: {
        label: 'name'
      },
      roleId: null, // 记录当前分配权限的角色id
      selectCheck: [] // 记录当前角色拥有的权限
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 获取角色列表数据
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    changePage(newPage) {
      this.page.page = newPage
      this.getRoleList()
    },
    // 获取公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    // 根据id删除角色
    async deleteRole(id) {
      try {
        await this.$confirm('确定删除该角色?')
        await deleteRole(id)
        this.getRoleList()
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 修改角色
    async editRole(id) {
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true
    },
    // 点击确定提交 添加 / 修改角色
    async roleSubmit() {
      try {
        await this.$refs.roleForm.validate()
        // validate()不传参的情况会返回Promise对象，只有校验通过才会执行await下方的代码
        if (this.roleForm.id) {
          // 编辑
          await updateRole(this.roleForm)
          this.$message.success('修改成功')
        } else {
          // 新增
          await addRole(this.roleForm)
          this.$message.success('添加成功')
        }
        this.getRoleList()
        this.showDialog = false
      } catch (error) {
        this.$message.error('校验失败')
      }
    },
    cancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 分配权限弹出层
    async assignPerm(id) {
      this.permData = tranListToTreeData(await getPermissionList(), '0')
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      this.selectCheck = permIds
      this.showPerDialog = true
    },
    async permSubmit() {
      await assignPerm({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.roleId })
      this.$message.success('分配权限成功')
      this.showPerDialog = false
    },
    permCancel() {
      this.selectCheck = []
      this.showPerDialog = false
    }
  }
}
</script>

<style>

</style>
